<?php

namespace App\Controllers\Experience;

use App\Models\Goods\Goods;
use App\Models\Experience\ExpGoods;
use App\Libs\Helper\CommonHelper;
use App\Models\Experience\ExpOrder;
use App\Libs\Base\Constant;
use App\Models\Goods\GoodsImg;
use App\Models\Goods\GoodsAffiliate;
use App\Models\Store\Store;

class GetExpGoodsDetail extends \App\Libs\Base\BaseAction
{
    protected function beforeRun()
    {
        parent::beforeRun();
//        $this->needLogin = TRUE;
    }

    /*
     * 体验商品，详情页
        latitude:纬度
        longitude:经度
     */
    public function execute()
    {
        if ($this->request->has('eid')) {
            $eid = $this->request->get('eid');
            $eDetail = ExpGoods::find([
                'conditions' => 'id = ?1',
                'columns' => 'id as eid,num as havenum,gid,end_time',
                'bind' => [1 => $eid]
            ])->toArray();
            //错误判断
            if (empty($eDetail)) {
                return \App\Libs\Views\Json::view(1, '指定的体验商品不存在', null);
            }

            //获得已申请数量
            $have_apply = ExpOrder::count([
                'conditions' => 'eid=?1',
                'bind' => [1 => $eid]
            ]);
            $eDetail['have_apply'] = $have_apply;
            //获得对应的商品信息
            $gid = $eDetail['gid'];
            $eGoodsInfo = Goods::find([
                'conditions' => 'id = ?1',
                'columns' => 'id as gid,sid,goods_name,goods_price,sale_price,goods_img,goods_desc',
                'bind' => [1 => $gid]
            ])->toArray();
            $sid = $eGoodsInfo['sid'];
            unset($eGoodsInfo['gid']);
            $eDetail = array_merge($eDetail, $eGoodsInfo);
            //获得商品额外的信息
            $extraInfo = GoodsAffiliate::find([
                'conditions' => 'gid=?1',
                'columns' => 'introduce,detail,xuzhi',
                'bind' => [1 => $gid]
            ])->toArray();
            $eDetail = array_merge($eDetail, $extraInfo);
            //获得店铺信息
            $storeInfo = Store::find([
                'conditions' => 'id=?1',
                'columns' => 'store_name,store_tel,store_address,longitude,latitude,store_info,store_img',
                'bind' => [1 => $sid]
            ])->toArray();
            $eDetail = array_merge($eDetail, $storeInfo);
            //获得距离
			$eDetail['distance'] = '0Km';
            if ($this->request->has('longitude') && $this->request->has('latitude')) {
                $distance = CommonHelper::getDistance($this->request->get('latitude'), $this->request->get('longitude'), $storeInfo['latitude'], $storeInfo['longitude']);
                $eDetail['distance'] = $distance;
            }
			//图片
			$img = GoodsImg::find(array(
				'gid=:gid: and show_status = 2',
				'bind' => array('gid'=>$gid),
			));
			if(!empty($img)){
				$imgUrl = json_decode($img->imgurl);
			}else{
   				$imgUrl = [];
			}
			
            unset($eDetail['latitude']);
            unset($eDetail['longitude']);
            unset($eDetail['sid']);
//            unset($eDetail['gid']);

			$eDetail['xuzhi'] = json_decode( $eDetail['xuzhi'] , true );
			$eDetail['img'] = $imgUrl;
			$eDetail['end_time'] = strtotime( $eDetail['end_time'] );
			$eDetail['goods_price'] = Goods::formatPrice($eDetail['goods_price']);
			$eDetail['sale_price'] = Goods::formatPrice($eDetail['sale_price']);
            return \App\Libs\Views\Json::view(0, '成功', $eDetail);

        } else {
            return \App\Libs\Views\Json::view(1, '请指定体验商品id', null);
        }
    }
}
